package Databases

import(
	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
	"fmt"
	"time"
	"gin/Config"
)

//数据库指针
var Db *sqlx.DB

//初始化数据库连接，init()方法系统会在动在main方法之前执行。
func init() {
	db_link := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s",
				Config.CONFIG.Db.User,
				Config.CONFIG.Db.Pwd,
				Config.CONFIG.Db.Host,
				Config.CONFIG.Db.Port,
				Config.CONFIG.Db.DatabaseName)
	//database, err := sqlx.Open("mysql", "root:ms_mima345@tcp(127.0.0.1:3306)/gushifenxi")
	fmt.Println("dblink:, ", db_link)
	database, err := sqlx.Open("mysql", db_link)
	if err != nil {
		fmt.Println("open mysql failed,", err)
	}
	Db = database
	Db.SetConnMaxLifetime(1 * time.Minute)
	Db.SetMaxOpenConns(100)
	Db.SetMaxIdleConns(50)
}
